// Copyright 2007-2014 Metaio GmbH. All rights reserved.
package com.metaio.Template;

import java.io.File;

import android.os.Bundle;

import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.jni.ETRACKING_STATE;
import com.metaio.sdk.jni.EVISUAL_SEARCH_STATE;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.IVisualSearchCallback;
import com.metaio.sdk.jni.ImageStruct;
import com.metaio.sdk.jni.Rotation;
import com.metaio.sdk.jni.TrackingValuesVector;
import com.metaio.sdk.jni.Vector3d;
import com.metaio.sdk.jni.VisualSearchResponseVector;
import com.metaio.tools.io.AssetsManager;

public class Template extends ARViewActivity 
{

	private IGeometry mStreet;
	
	private MapTransitionHelper mapTransitionHelper;
	
	private MetaioSDKCallbackHandler mSDKCallback;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		mapTransitionHelper=new MapTransitionHelper();
		mSDKCallback = new MetaioSDKCallbackHandler();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		setTrackingConfiguration();
	}

	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		mSDKCallback.delete();
		mSDKCallback = null;
	}
	
	@Override
	protected int getGUILayout() 
	{
		// Attaching layout to the activity
		return R.layout.template; 
	}
	
	@Override
	protected void loadContents() 
	{
		mSDKCallback=new MetaioSDKCallbackHandler();
		
		final File modelPath = AssetsManager.getAssetPathAsFile(
				getApplicationContext(), "Tiger.md2");

		if (modelPath != null) {
			mStreet = metaioSDK.createGeometry(modelPath);
			mStreet.setTranslation(new Vector3d(0, 0, -300));
			mStreet.setVisible(true);
			mStreet.setCoordinateSystemID(1);
		}
		
		metaioSDK.setRendererClippingPlaneLimits(10, 30000);
	}

	@Override
	protected void onGeometryTouched(IGeometry geometry) 
	{
		MetaioDebug.log("Template.onGeometryTouched: " + geometry);
	}


	@Override
	protected IMetaioSDKCallback getMetaioSDKCallbackHandler() 
	{
		return mSDKCallback;
	}
	
	final class MetaioSDKCallbackHandler extends IMetaioSDKCallback 
	{

		@Override
		public void onSDKReady() 
		{
			MetaioDebug.log("The SDK is ready");
		}
		
		@Override
		public void onAnimationEnd(IGeometry geometry, String animationName) 
		{
			MetaioDebug.log("animation ended" + animationName);
		}
		
		@Override
		public void onMovieEnd(IGeometry geometry, File filePath)
		{
			MetaioDebug.log("movie ended" + filePath.getPath());
		}
		
		@Override
		public void onNewCameraFrame(ImageStruct cameraFrame)
		{
			update();
		}
		
		@Override 
		public void onCameraImageSaved(File filePath)
		{
			MetaioDebug.log("a new camera frame image is saved to" + filePath.getPath());
		}
		
		@Override
		public void onScreenshotImage(ImageStruct image)
		{
			MetaioDebug.log("screenshot image is received" + image.getTimestamp());
		}
		
		@Override
		public void onScreenshotSaved(File filePath)
		{
			MetaioDebug.log("screenshot image is saved to" + filePath.getPath());
		}
		
		@Override
		public void onTrackingEvent(TrackingValuesVector trackingValues)
		{
			if(trackingValues.isEmpty()){
				return;
			}
			if(trackingValues.get(0).getState()==ETRACKING_STATE.ETS_LOST||trackingValues.get(0).getState()==ETRACKING_STATE.ETS_INITIALIZATION_FAILED){
				mapTransitionHelper.prepareForTransitionToNewMap();
				setTrackingConfiguration();
			}
		}

		@Override
		public void onInstantTrackingEvent(boolean success, File filePath)
		{
			if (success)
			{
				metaioSDK.setTrackingConfiguration(filePath);
			}
		}
	}
	
	final class VisualSearchCallbackHandler extends IVisualSearchCallback
	{
		@Override
		public void onVisualSearchResult(VisualSearchResponseVector response, int errorCode)
		{
			if (errorCode == 0)
			{
				MetaioDebug.log("Visual search is successful");
			}
		}

		@Override
		public void onVisualSearchStatusChanged(EVISUAL_SEARCH_STATE state) 
		{
			MetaioDebug.log("The current visual search state is: " + state);
		}
	}
	
	private void setTrackingConfiguration() {
		// TODO Auto-generated method stub
		final File trackingConfigFile = AssetsManager.getAssetPathAsFile(
				getApplicationContext(), "tracking.xml");
		metaioSDK.setTrackingConfiguration(trackingConfigFile);
	}

	public void update() {
		// TODO Auto-generated method stub
		mapTransitionHelper.update(metaioSDK.getTrackingValues(1), metaioSDK
				.getRegisteredSensorsComponent().getLastSensorValues());
		
		if(mapTransitionHelper.lastFrameWasTracked()){
			Rotation rotation=mapTransitionHelper.getRotationCameraFromWorld();
			Vector3d translation=mapTransitionHelper.getTranslationCameraFromWorld();
			
			mStreet.setRotation(rotation, true);
			mStreet.setTranslation(translation, true);
		}
	}
}
